package com.abc.filter;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 第四步：在配置到指定的路由中去
 *
 * 如果所有的路由都需要，则自定义过滤器中可以直接实现全局过滤器，就无需配置到指定路由中去了，如：GlobalCacheRequestBodyFilter。
 *
 * @Description 网关配置，全局过滤器在全部的路由中都生效，各个路由中可配置指定的过滤器，仅对单个路由生效。
 */
@Configuration
public class GatewayConfig {

    /**
     * 全局过滤器：缓存请求body
     */
    @Bean
    public GlobalCacheRequestBodyFilter globalCacheRequestBodyFilter(){
        return new GlobalCacheRequestBodyFilter(-30);
    }

    @Bean
    public RouteLocator appRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/test/login")
                        .filters(f -> f.filter(new AppReqDecryptFilter(-25))   // GlobalCacheRequestBodyFilter > order > GlobalLogFilter
                                .filter(new AppRespEncryptFilter(-22))         // AppRespEncryptFilter < -1 && AppRespEncryptFilter > GlobalLogFilter
                                //.filter(new AppTokenFilter(-10))               // AppTokenFilter > AppReqDecryptFilter
                        )
                        .uri("http://127.0.0.1:8080")
                        .id("router1")
                ).build();
    }
}
